package com.seari.custody.controller;

import com.seari.custody.bean.Role;
import com.seari.custody.constant.MessageConstant;
import com.seari.custody.pojo.MessageBean;
import com.seari.custody.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询角色信息
     * @param request
     * @renturn
     */

    @RequestMapping("/getAllRole")
    @ResponseBody
    public MessageBean getAllRole(HttpServletRequest request) {

        MessageBean messageBean = new MessageBean();
        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            List<Role> lists = roleService.getAllById(null);
            dataMap.put("date",lists);
            messageBean.setType(MessageConstant.SUCCESS_TYPE);
            messageBean.setCode(MessageConstant.SUCCESS);
            messageBean.setMessage("信息获取成功 ");
            messageBean.setData(dataMap);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("信息获取失败");
        }
        return messageBean;
    }


    /**
     * 新增角色信息
     */
    @RequestMapping("getAddRole")
    @ResponseBody
    public MessageBean getAddRole (Role ad){
        MessageBean messageBean = new MessageBean();
        int resultNum = 0;
        resultNum = roleService.getAddRole(ad);
        if (resultNum > 0){
            messageBean.setType(MessageConstant.SUCCESS);
            messageBean.setCode(MessageConstant.SUCCESS_TYPE);
            messageBean.setMessage("新增角色成功");
        }else {
            messageBean.setType(MessageConstant.FAIL);
            messageBean.setCode(MessageConstant.FAIL_TYPE);
            messageBean.setMessage("新增角色成功");
        }
        return messageBean;
    }


    /**
     * 角色信息编辑
     */

    @RequestMapping("getUpdateRole")
    @ResponseBody
    public MessageBean getUpdateRole (Role et){
        MessageBean messageBean = new MessageBean();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<>();
        params.put("id", et.getId());
        params.put("user_cn", et.getNameCn());
        params.put("user_en", et.getNameEn());
        params.put("type", et.getType());
        int resultNum = 0;
        resultNum = roleService.getUpdateRole(params);
        if (resultNum > 0) {
            dataMap.put("data", "成功");
        }
        try{
            dataMap.put("ceshi", "成功内容");
            messageBean.setType(MessageConstant.SUCCESS);
            messageBean.setCode(MessageConstant.SUCCESS_TYPE);
            messageBean.setMessage("角色编辑成功");
        }catch (Exception e){
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL);
            messageBean.setCode(MessageConstant.FAIL_TYPE);
            messageBean.setMessage("角色编辑失败");
        }
        return messageBean;
    }


    /**
     * 删除角色信息
     */

    @RequestMapping(value = "deleteRole")
    @ResponseBody
    public MessageBean deleteRole(String id) {
        MessageBean messageBean = new MessageBean();
        int resultNum = 0;
        resultNum = roleService.deleteRole(id);
        if (resultNum > 0){
            messageBean.setType(MessageConstant.SUCCESS);
            messageBean.setCode(MessageConstant.SUCCESS_TYPE);
            messageBean.setMessage("角色删除成功");
        }else {
            messageBean.setType(MessageConstant.FAIL);
            messageBean.setCode(MessageConstant.FAIL_TYPE);
            messageBean.setMessage("角色删除失败");
        }
        return messageBean;
    }

}
